<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@ page import="java.sql.*"%>
<%@ page import="cleanstarxchange.server.*"%>
<%@ page import="javax.jdo.PersistenceManager"%>
<%@ page import="java.util.ArrayList"%>
<%@ page import="java.util.List"%>    
<%@ page import="java.text.DecimalFormat"%>
    
    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
	
<%	
	Integer uid = Integer.parseInt(request.getParameter( "uid" ));
	Integer updateid = Integer.parseInt(request.getParameter( "updateid" ));
%>
	
	
	<script language="javascript">
	<!--
	function moveToHome()
	{
   		var frm = document.frm;
	   	frm.action = "index.jsp?uid=<%=uid%>";
   		frm.submit();
	}

	function moveToPF()
	{
   		var frm = document.frm;
	   	frm.action = "portfolio.jsp?uid=<%=uid%>";
   		frm.submit();
	}

	function moveToUpdate()
	{
   		var frm = document.frm;
	   	frm.action = "update.jsp?uid=<%=uid%>&updateid=1";
   		frm.submit();
	}
	//-->
	</script>
	
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<title>Star XChange</title>
		<link rel="stylesheet" href="css/style.css" type="text/css" media="screen" />
		
		<!--[if IE]>
		<style type="text/css"> 
		/* place css fixes for all versions of IE in this conditional comment */
		.twoColElsLtHdr #sidebar1 { padding-top: 30px; }
		.twoColElsLtHdr #mainContent { zoom: 1; padding-top: 15px; }
		/* the above proprietary zoom property gives IE the hasLayout it needs to avoid several bugs */
		</style>
		<![endif]-->
	</head>

	<body class="twoColElsLtHdr">
	<%
		DataStore ds = new DataStore();
	
		
		if (!ds.isInitialized()){
			Initializer.Initialize();
			//ds.initialize();
		}
		
//		if (!ds.isUpdateInitialized()){
//			ds.initializeUpdate();
//		}

		
	    //ArrayList<Stock> stocks = ds.getStock();
	    ArrayList<Stock> stocks = new ArrayList<Stock>();
	    stocks.add(ds.getStock( new Long(updateid) ));
			
		//Do today's update
		
		//Kevin's little algorithm
		DecimalFormat twoDForm = new DecimalFormat("#.##");
		
		int[] scores = new int[10];
		
		for(int i=0; i < stocks.size();i++){
			int score=0;
			double newPrice=0;		
			boolean value;
			
			score = TweetCounter.getCount(stocks.get(i).getName());	
			System.out.println("Score for one person Done!" + score);
			
			
			//score=40;
			
			newPrice=stocks.get(i).getCurrentPrice() + ((score*0.75 - stocks.get(i).getCurrentPrice())/100);
			newPrice = Double.valueOf(twoDForm.format(newPrice));
			stocks.get(i).updatePrice(newPrice);
			
			//ds.updateStock(stocks.get(i));
		}
		
		if ( updateid == 61 ){
			ArrayList<User> users = ds.getUser();
			for ( int i = 0; i < users.size(); i++ )
				ds.computeAssetValue( users.get(i) );
		}
		
		ds.close();
		
		
		System.out.println("Score has been fetched");
		
    %>
	<div id="container">
  		
  		<div id="header"><a href="index.jsp?uid=<%=uid%>"><img src="media/logo.jpg"/></a>
  		<!-- end #header -->
  		</div>
  		
  		<div id="sidebar1">
   			<ul class="sidenav">
				<li>
                	<a href="portfolio.jsp?uid=<%=uid%>" title="My Portfolio">My Portfolio
                    <span>See how rich I am</span>
                    </a>
                    
                </li>
			    <li>
                	<a href="stocklist.jsp?uid=<%=uid%>" title="Stocks">Stocks
                    <span>See the market flow</span>
                    </a>
                </li>
				<li>
                	<a href="rank.jsp?uid=<%=uid%>" title="Rank">Rank
                    <span>Who is the winner now</span>
                    </a>
                </li>
				<li>
                	<a href="howtoplay.jsp?uid=<%=uid%>" title="HowToPlay">How To Play
                    <span>Learn how to play the game</span>
                    </a>
                </li>
			</ul>
    	<!-- end #sidebar1 -->
    	</div>
  		
  		<div id="mainContent">
    		<div id="shortcut">
    			<form name="frm" method="post">
				<input type="button" name="btregister" value = "Home" onClick="moveToHome();">
				<input type="button" name="btregister" value = "Portfolio" onClick="moveToPF();">
				<% if ( uid==713865194 || uid==21014218 || uid==783920693 || uid==514766462 && updateid == 61 ){ %>
				<input type="button" name="btregister" value = "Update Again :)" onClick="moveToUpdate();">
				<% } %>
				</form>
    		</div>
    		<div id="shortcut">
    			<% if ( updateid == 61 ){ %>
    			<h1>Update is done now!</h1>
    			<% } else { %>
    			Price of <%= stocks.get(0).getName() %> updated.
    			<script language="JavaScript">
									location.replace("update.jsp?uid=<%=uid%>&updateid=<%=updateid+1%>");
								</script>
    			<% } %>
				<h3></h3>
    			
    		</div>
    		<!-- end #mainContent -->
    	</div>
		
		<!-- This clearing element should immediately follow the #mainContent div in order to force the #container div to contain all child floats -->
		<br class="clearfloat" />
   		
   		<div id="footer">
    		<p>Ranks will be here</p>
  		<!-- end #footer -->
  		</div>
  		
	<!-- end #container -->
	</div>
	
	</body>
</html>
